﻿//using Grpc.Net.Client;
using DouDou.Shop.Common.Models;
using DouDou.Shop.Interface;
using DouDou.Shop.Model;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;

namespace DouDou.Shop.UserMicroservice.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly ILogger<UserController> _logger;
        private readonly IConfiguration _iConfiguration = null;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="logger"></param>
        /// <param name="configuration"></param>
        public UserController(IUserService userService, ILogger<UserController> logger,
            IConfiguration configuration)
        {
            this._userService = userService;
            this._logger = logger;
            this._iConfiguration = configuration;
        }

        /// <summary>
        /// 检查信息格式
        /// </summary>
        /// <param name="data"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        //[Route("check/{data}/{type}")]
        //[HttpGet]
        //public JsonResult CheckData(string data, int type)
        //{
        //    return new JsonResult(_userService.CheckData(data, type));
        //}

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        //[Route("send")]
        //[HttpPost]
        //public JsonResult SendVerifyCode([FromForm] string phone)
        //{
        //    //检查的时候，需要 ip--
        //    AjaxResult ajaxResult = this._userService.CheckPhoneNumberBeforeSend(phone);
        //    if (!ajaxResult.Result)//校验失败
        //    {
        //        return new JsonResult(ajaxResult);
        //    }
        //    else
        //    {

        //        return new JsonResult(_userService.SendVerifyCode(phone));
        //    }
        //}

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="user"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        //[Route("Register")]
        //[HttpPost]
        ////[TypeFilter(typeof(CustomAction2CommitFilterAttribute))]
        //public JsonResult Register([FromForm] TbUser user, [FromForm] string code)
        //{
        //    _userService.Register(user, code);
        //    return new JsonResult(new AjaxResult()
        //    {
        //        Result = true,
        //        Message = "注册成功"
        //    });
        //}

        /// <summary>
        /// 【根据用户名和密码查询用户是否合法】
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [Route("Query")]
        [HttpGet]
        public JsonResult QueryUser(string userName, string password)
        {
            AjaxResult<TbUser> ajaxResult = null;
            TbUser tbUser = _userService.QueryUser(userName, password);
            ajaxResult = new AjaxResult<TbUser>()
            {
                Result = true,
                TValue = tbUser
            };
            return new JsonResult(ajaxResult);
        }


        /// <summary>
        /// 【验证请求在hearder里带上token后，是否能够通过Authorize】
        /// </summary>
        /// <returns></returns>
        [Route("TestAuthorize")]
        [Authorize]
        [HttpGet]
        public string TestAuthorize()
        {
            return "TestAuthorize";
        }

        /**
	    * 根据用户名和密码查询用户
	    * @param username
	    * @param password
	    * @return
	    */
        //[Route("/api/user/verify")]
        //[HttpGet]
        //[AllowAnonymousAttribute]//自己校验
        //public JsonResult CurrentUser()
        //{
        //    //if (new Random().Next(1, 100) > 50)
        //    //    throw new Exception("这里我刻意增加了一个非常特别的异常，你注意到了吗？50%的概率");
        //    AjaxResult ajaxResult = null;
        //    IEnumerable<Claim> claimlist = HttpContext.AuthenticateAsync().Result.Principal.Claims;
        //    if (claimlist != null && claimlist.Count() > 0)
        //    {
        //        string username = claimlist.FirstOrDefault(u => u.Type == "username").Value;
        //        string id = claimlist.FirstOrDefault(u => u.Type == "id").Value;
        //        ajaxResult = new AjaxResult()
        //        {
        //            Result = true,
        //            Value = new
        //            {
        //                id = id,
        //                username = username,
        //            }
        //        };
        //    }
        //    else
        //    {
        //        ajaxResult = new AjaxResult()
        //        {
        //            Result = false,
        //            Message = "Token无效，请重新登陆"
        //        };
        //    }
        //    return new JsonResult(ajaxResult);
        //}

    }
}
